﻿#pragma once

#include <QtWidgets/QWidget>
#include <QWebEngineView>
#include <QVBoxLayout>
#include <QWebEngineSettings>
#include <QtWebChannel>
#include <QMessageBox>
#include <QKeyEvent>
#include <QDebug>
#include <QPushButton>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QLabel>

#include <QSettings>
#include "custom_page.h"



class WebClass;
class Button;
class close_dialog;


class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget* parent = Q_NULLPTR);
    ~MainWidget();

    void onFeaturePermissionRequestedSlot(const QUrl& securityOrigin, QWebEnginePage::Feature feature);

    void user_show_normal(bool is_hit=false);
    void user_show_max();
signals:
    void service_inited_sig();
protected:
    virtual void mousePressEvent(QMouseEvent* event);
    virtual void mouseMoveEvent(QMouseEvent* event);

    virtual void keyPressEvent(QKeyEvent* event);

#ifdef WIN32
    bool nativeEvent(const QByteArray& eventType, void* message, long* result);
#endif

private:
    void createMenu();

    void switch_full();

    void show_version();

    void on_close_btn_clicked();
    void on_close_diglog_accept();
    void on_close_diglog_finish();


    void on_service_inited(int port);
    void service_inited_slot();

private:
    int boundaryWidth = 4;
    QWidget* central_widget;
    QWidget* title_widget;

    QPoint m_lastPos;

    QWebEngineView* webview_;
    QWebChannel* webchannel_;
    WebClass* web_class_;
    CustomPage* custom_page_;

    int http_port_;

    /* 标题栏功能 */
    Button* closebtn_;
    Button* maxbtn_;
    Button* minbtn_;
    Button* fullbtn_;
    
    QLabel* logo_label_;
    QLabel* title_label_;
    QLabel* version_label_;
    QPoint originalSize_;

    // 托盘
    QSystemTrayIcon* systemtrayicon_;
    QMenu* menu_;

    bool is_max_;

    bool is_full_;
    bool web_debug_;

    QPixmap max_pixmap_;
    QPixmap normal_pixmap_;

    close_dialog* close_dia_;

    QSettings* setting_;
    friend class WebClass;

    int window_width_;
    int window_height_;
    QString load_url_;
private slots:
    void slot_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason);
    void slot_exitAppAction();
};

class Button : public QPushButton {
    Q_OBJECT

public:
    Button(QWidget* parent = Q_NULLPTR);

protected:
    virtual void mousePressEvent(QMouseEvent* event);
    virtual void mouseMoveEvent(QMouseEvent* event);
    virtual void mouseReleaseEvent(QMouseEvent* event);


private:
    QPoint click_pos;
    bool ismove;
};
